package com.xcw.bb.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;


public class JdbcUtil {
private static Properties  map = new Properties();
	
	static{
		InputStream fis = null;
		try {
			fis = JdbcUtil.class.getResourceAsStream("/com/zpark/bb/conf/db.properties");
			map.load(fis);
		} catch (Exception e) {
	         e.printStackTrace();
		}finally{
			if(fis!=null) try{fis.close();}catch(Exception e){}
		}
	}

	private static ThreadLocal<Connection> tol = new ThreadLocal<Connection>();
	
	public static Connection getConnection() throws Exception{
		Connection conn = tol.get();
		if( conn==null ){
		    Class.forName( map.getProperty("driver") );
		    conn = DriverManager.getConnection( map.getProperty("url") , map.getProperty("username") , map.getProperty("password") );
		    tol.set(conn);
		}
		return conn;
	}
	
	public static void close(ResultSet rs,Statement stm,Connection conn){
		  if(rs!=null) try{ rs.close();}catch(Exception e){}
		  if(stm!=null) try{ stm.close(); }catch(Exception e){}
		  if(conn!=null) try{ conn.close();  tol.remove(); } catch(Exception e){}
	}
}
